/**
 * 
 */
var params = {
	pageSize : 10
};

var logTable;// 无参数
$(function() {
	tabModel();
	addsysinfo();
	commitBtnClickEvent();
	showDetail();
	updateInfo();
	remove();
	LTS.colFormatter.optFormat = function(v, row) {
		return generateOpt(row);
	}
	LTS.colFormatter.typeFormat = function(v, row) {
		return generateType(row);
	}
	LTS.colFormatter.statusFormat = function(v, row) {
		return generateStatusText(row);
	}
	logTable = $("#log-table-container").ltsTable({
		url : getUrlBySuffix("/syslog/getLogList"),
		templateId : 'log-table'
	});
	logTable.post(params, 1);
	
	deleteBtnClick();
})

function tabModel() {
	$(document).on("click", ".choiceBtn", function() {
		var thisid = $(this).attr("id");
		$(".menuContent").hide();
		$("." + thisid).show();
	})
}

function addsysinfo() {
	$(document).on("click", "#addinfo", function() {
		var _temphtml = $("#addsysinfo_template").html();
		$("#systemlog-insert-form").find(".addsysinfo").after(_temphtml);
	})
}

function commitBtnClickEvent() {
	$(document).on("click", "#commitBtn", function() {
		var url = getUrlBySuffix("/syslog/saveSystemUpdateLog");
		var params = getParam();
		if (checkParam(params)) {
			$.post(url, params, function(json) {
				if (json && json.success) {
					swal("成功", json.message, "success");
					window.location.reload();
				} else {
					swal("提示", json.message, "error");
				}
			});
		}
	})
}

function checkParam(params) {
	for ( var p in params) {
		if (isNull(params[p]) || params[p] == false) {
			swal("警告", "表单内容有误请检查", 'warning');
			return false;
		}
	}
	return true;
}
function getParam() {
	var params = {}
	params.addtime = $.trim($("input[name=deadline]").val());
	params.loglist = getloglist();
	params.status = $(".push option:selected").val();
	return params;
}
function getloglist() {
	var loglistObj = $("#systemlog-insert-form").find(".addsysinfo");
	if (loglistObj.length > 0) {
		var loglist = "[";
		for (var i = 0; i < loglistObj.length; i++) {
			var thisobj = loglistObj[i];
			var title = $.trim($(thisobj).find("input[name=name]").val());
			var context = $.trim($(thisobj).find(".mainContext").val());
			var type = $(thisobj).find("input[name=methodType]:checked").val();
			var _temp = '{"title":"' + title + '","context":"' + context
					+ '","type":"' + type + '"},';
			loglist += _temp;
		}
		loglist = loglist.substring(0, loglist.lastIndexOf(","));
		loglist += "]";
		return loglist;
	} else {
		return false;
	}

}

/**
 * 生成按钮
 */
function generateOpt(jsonObj) {
	var optButtons = generateStatus(jsonObj)
			+ '<a href="javascript:;" class="show-btn">'
			+ '<span class="label label-success" style="font-size: 14px">'
			+ '<i class="fa fa-search"></i>&nbsp;&nbsp;' + '查看详细' + '</span>'
			+ '</a>&nbsp;' + '<a href="javascript:;" class="delete-btn">'
			+ '<span class="label label-success" style="font-size: 14px">'
			+ '<i class="fa fa-search"></i>&nbsp;&nbsp;' + '删除' + '</span>'
			+ '</a>&nbsp;';

	return optButtons;
}

/**
 * 生成状态
 */
function generateStatus(jsonObj) {
	var optButtons = '<a href="javascript:;" class="status-btn" jsontype="'
			+ jsonObj.logStatus + '">'
			+ '<span class="label label-success" style="font-size: 14px">'
			+ '<i class="fa fa-search"></i>&nbsp;&nbsp;' + ''
			+ (jsonObj.logStatus == "2" ? "下架" : "上架") + '</span>'
			+ '</a>&nbsp;';

	return optButtons;
}
/**
 * 生成类型
 */
function generateType(jsonObj) {
	if (jsonObj.logScope == "1") {
		return "首页";
	} else {
		return "GWEBSQL";
	}
}
/**
 * 生成类型
 */
function generateStatusText(jsonObj) {
	if (jsonObj.logStatus == "1") {
		return "下架";
	} else {
		return "上架";
	}
}

function showDetail() {
	$(document).on(
			"click",
			".show-btn",
			function() {
				var logId = getCurrentId($(this));
				var param = {
					id : Number(logId)
				}
				var url = getUrlBySuffix("/syslog/getlog");
				$.post(url, param, function(json) {
					if (json && json.success) {
						var obj = json.list[0];
						$("input[name=id]").val(obj.id);
						$("input[name=title]").val(obj.logTitle);
						$("input[name=addTimeNode]").val(obj.addTimeNode);
						$("input[name=type]").val(
								obj.logScope == "1" ? "首页" : "GWEBSQL");
						$("input[name=updater]").val(obj.updateInputer);
						$(".descriptor").html(obj.logContent);
						$("#log-modal").modal("show");
					} else {

					}

				})

			})
}

function updateInfo() {
	$(document).on("click", ".status-btn", function() {
		var status = $(this).attr("jsontype");
		var logId = getCurrentId($(this));
		var param = {}
		param.id = logId;
		if (status == "2") {
			param.logStatus = 1;
		} else {
			param.logStatus = 2;
		}
		var url = getUrlBySuffix("/syslog/updata");
		$.post(url, param, function(json) {
			if (json && json.success) {
				swal("成功", json.message, "success");
				logTable.post(params, pageNum);
			} else {
				swal("提示", json.message, "error");
			}
		})

	})
}

function remove() {
	$(document).on("click", ".removeAttr", function() {
		var loglistObj = $("#systemlog-insert-form").find(".addsysinfo");
		if (loglistObj.length > 1) {
			$(this).parent("div").parent(".form-group").parent().remove();
		}
	})
}

function deleteBtnClick() {
	$(document).on("click", ".delete-btn", function() {
		var id = getCurrentId($(this));
		var param = {}
		param.id = id;
		param.logStatus = 2;
		var url = getRoot() + '/syslog/deleteLogs';
		$.post(url, {
			"id" : id
		}, function(data) {
			if (data.success) {
				swal("成功", data.message, 'success');
				logTable.post(params, pageNum);
			} else {
				swal('失败', data.message, 'error');
			}
		});
	});
}